#ifndef HTTP_CONTEXT_H
#define HTTP_CONTEXT_H

#include "./HttpRequest.h"

class Buffer;

class HttpContext
{
public:
    // 请求解析的状态，状态转换控制
    enum class RequestParseState
    {
        ExpectRequestLine,
        ExpectHeaders,
        ExpectBody,
        GotAll
    };

    bool parseRequest(Buffer *buffer, Timestamp receiveTime);
    HttpRequest getRequest() const
    {
        return this->request_;
    }

    bool isRequestParseStateGotAll() const
    {
        return this->requestParseState_ == RequestParseState::GotAll;
    }

    // 重置HttpContext
    // 异常安全（要么成功要么失败不变；因为只有基本类型赋值、基本类型交换、容器交换）
    void reset()
    {
        this->requestParseState_ = RequestParseState::ExpectRequestLine;
        // 临时对象传给当前对象置空，临时对象再析构
        HttpRequest tmp{};
        this->request_.swap(tmp);
    }

private:
    bool praseRequestLine(const char *begin, const char *end);

    RequestParseState requestParseState_ = RequestParseState::ExpectRequestLine;
    HttpRequest request_;
};

#endif // HTTP_CONTEXT_H